package com.ruoyi.drug.service.impl;

import com.ruoyi.drug.domain.DrugPhysic;
import com.ruoyi.drug.domain.DrugPhysics;
import com.ruoyi.drug.domain.DrugRecipt;
import com.ruoyi.drug.domain.DrugReciptPhysics;
import com.ruoyi.drug.mapper.DrugPhysicMapper;
import com.ruoyi.drug.mapper.DrugReciptMapper;
import com.ruoyi.drug.service.IDrugReciptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrugReciptServiceImpl implements IDrugReciptService {
    @Autowired
    private DrugReciptMapper drugReciptMapper;
    @Autowired
    private DrugPhysicMapper drugPhysicMapper;

    @Override
    public List<DrugPhysics> selectPhysicsList(DrugPhysics drugPhysics) {
        return drugReciptMapper.selectPhysicsList(drugPhysics);
    }

    @Override
    public int addReciptPhysic(Long[] ids,String reciptNum) {
        int i= 0;
        for (Long id: ids){
            drugReciptMapper.addReciptPhysic(id,reciptNum);
            i++;
        }
        return i;
    }

    @Override
    public List<DrugReciptPhysics> selectReciptPhysics(String reciptNum) {
        return drugReciptMapper.selectReciptPhysics(reciptNum);
    }

    @Override
    public int addRecipt(DrugRecipt drugRecipt) {
        List<DrugRecipt> list = drugReciptMapper.selectRecipt(new DrugRecipt());
        for (DrugRecipt recipt : list) {
            if (drugRecipt.getReciptNum().equals(recipt.getReciptNum())){
                return drugReciptMapper.updateRecipt1(drugRecipt);
            }
        }
        if(drugRecipt.getBuySupplier()==null || drugRecipt.getCreateTime()==null){
            return 0;
        }
        return drugReciptMapper.addRecipt(drugRecipt);
    }

    @Override
    public int addRecipt1(DrugRecipt drugRecipt) {
        List<DrugRecipt> list = drugReciptMapper.selectRecipt(new DrugRecipt());
        for (DrugRecipt recipt : list) {
            if (drugRecipt.getReciptNum().equals(recipt.getReciptNum())){
                return drugReciptMapper.updateRecipt(drugRecipt);
            }
        }
        if(drugRecipt.getBuySupplier()==null || drugRecipt.getCreateTime()==null){
            return 0;
        }
        return drugReciptMapper.addRecipt2(drugRecipt);
    }

    @Override
    public List<DrugRecipt> selectRecipt(DrugRecipt drugRecipt) {
        return drugReciptMapper.selectRecipt(drugRecipt);
    }

    @Override
    public int updateSetting(Long[] ids, DrugReciptPhysics drugReciptPhysics) {
        int i = 0;
        for (Long id:ids){
            int rpCount = drugReciptPhysics.getRpCount();
            Double rpPrice = drugReciptPhysics.getRpPrice();
            Double rpTotal = rpCount*rpPrice;
            drugReciptMapper.updateSetting(id,rpCount,rpPrice,drugReciptPhysics.getRpNum(),drugReciptPhysics.getRemark(),rpTotal);
            i++;
        }
        return i;
    }

    @Override
    public int deletePhysic(Long pId) {
        return drugReciptMapper.deletePhysic(pId);
    }

    @Override
    public int deleteRecipt(Long[] ids) {
        int result = 0;
        for (Long id: ids){
            drugReciptMapper.deleteRecipt(id);
            result++;
        }
        return result;
    }

    @Override
    public int processRecipt(Long[] ids) {
        int result = 0;
        for (Long id: ids){
            drugReciptMapper.processRecipt(id);
            result++;
        }
        return result;
    }

    @Override
    public int reciptPutIn(Long rId,String username) {
        String reciptNum = drugReciptMapper.selectReciptNumByrId(rId);
        List<DrugReciptPhysics> list1 = drugReciptMapper.selectReciptPhysics(reciptNum);
        List<DrugPhysic> list2 = drugPhysicMapper.selectPhysicList(new DrugPhysic());
        for (DrugReciptPhysics drugReciptPhysics : list1) {
            boolean add = false;
            String code = drugReciptPhysics.getpCode();
            for (DrugPhysic drugPhysic : list2) {
                if (code.equals(drugPhysic.getPhysicCode())){
                    add=true;
                }
            }
            if (add){
                drugReciptMapper.physicPutIn(code,drugReciptPhysics.getRpCount());
            }else {
                DrugPhysic physic = new DrugPhysic();
                physic.setPhysicName(drugReciptPhysics.getpName());
                physic.setPhysicCode(drugReciptPhysics.getpCode());
                physic.setPhysicFactory(drugReciptPhysics.getpFactory());
                physic.setPhysicKeyword(drugReciptPhysics.getpKeyword());
                physic.setPhysicUnit(drugReciptPhysics.getpUnit());
                physic.setPhysicType(drugReciptPhysics.getpType());
                physic.setPhysicRecipeType(drugReciptPhysics.getpRecipeType());
                physic.setPhysicStorage(drugReciptPhysics.getRpCount());
                System.out.println(drugReciptPhysics);
                System.out.println(physic);
                drugPhysicMapper.addPhysic(physic);
            }
        }

        return drugReciptMapper.reciptPutIn(rId,username);
    }

    @Override
    public DrugRecipt selectReciptByRNum(String rNum) {
        return drugReciptMapper.selectReciptByRNum(rNum);
    }
}
